import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class Test {

    public static void main(String[] args) {
        String host = "172.16.1.89";
        String port = "3050";
        String databaseName = "HyNet";
        String user = "SYSDBA";
        String password = "masterkey";
        String jdbcUrl = "jdbc:firebirdsql://" + host + ":" + port + "/" + databaseName + "";

        Connection connection = null;

        try {
            Class.forName("org.firebirdsql.jdbc.FBDriver");
            connection = DriverManager.getConnection(jdbcUrl, user, password);
            System.out.println("数据库连接成功！");

            List<String> tableNames = queryTableNames(connection);

            for (String tableName : tableNames) {
                printTableData(connection, tableName);
            }

        } catch (Exception e) {
            System.err.println("连接失败，错误信息: " + e.getMessage());
            e.printStackTrace();
        } finally {
            IoUtil.close(connection);
        }
    }

    public static List<String> queryTableNames(Connection connection) {
        Statement statement = null;
        ResultSet resultSet = null;

        List<String> tableNames = new ArrayList<>();
        try {
            statement = connection.createStatement();

//         ## 查询有些什么表
            String sql = "SELECT RDB$RELATION_NAME as TABLE_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG = 0 ORDER BY RDB$RELATION_NAME";
            resultSet = statement.executeQuery(sql);
            int count = 0;
            while (resultSet.next()) {
                count++;
                String tableName = resultSet.getString("TABLE_NAME").trim();
                tableNames.add(tableName);
            }
            System.out.println("总计: " + count + " 张表：" + CollUtil.join(tableNames, ", "));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(statement);
            IoUtil.close(resultSet);
        }
        return tableNames;
    }

    public static void printTableData(Connection connection, String table) {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.createStatement();
            String sql = "SELECT FIRST 10 * FROM " + table;
            resultSet = statement.executeQuery(sql);

            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount(); // 获取总列数

            // 打印表头（列名）
            System.out.println();
            System.out.println("表：" + table);
            for (int i = 1; i <= columnCount; i++) {
                System.out.print(metaData.getColumnName(i) + "\t");
            }
            System.out.println();

            // 打印每一行的数据
            while (resultSet.next()) {
                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnName(i);
                    Object value = resultSet.getObject(i);
                    String valueStr = (value == null) ? "NULL" : value.toString();
                    System.out.print(valueStr + "\t");
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(statement);
            IoUtil.close(resultSet);
        }
    }

}
